package com.wuliupai.delivery.wuliupaidelivery;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

import com.baidu.mapapi.SDKInitializer;
import com.google.gson.JsonObject;
import com.umeng.message.IUmengRegisterCallback;
import com.umeng.message.PushAgent;
import com.umeng.message.UmengNotificationClickHandler;
import com.umeng.message.entity.UMessage;
import com.wuliupai.delivery.wuliupaidelivery.activity.OrderDetailActivity_;
import com.wuliupai.delivery.wuliupaidelivery.constants.PushBean;
import com.wuliupai.delivery.wuliupaidelivery.utils.JsonUtils;
import com.wuliupai.delivery.wuliupaidelivery.utils.LogUtils;
import com.wuliupai.delivery.wuliupaidelivery.utils.StringUtils;

import java.util.LinkedList;
import java.util.List;

/**
 * Descriptions：Application
 * <p>
 * Author：ChenME
 * Date：11/10/2016
 * Email：ibelieve1210@163.com
 */
public class App extends Application {

    public static Context mContext;
    public static Toast mToast = null;
    public static PushAgent pushAgent;
    private List<Activity> kList = new LinkedList<Activity>();
    private static App instance;

    private static Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (null == mToast) {
                mToast = Toast.makeText(mContext, msg.obj.toString(), Toast.LENGTH_SHORT);
            } else {
                mToast.setText(msg.obj.toString());
            }
            mToast.show();
        }
    };

    @Override
    public void onCreate() {
        super.onCreate();
        mContext = this;
        SDKInitializer.initialize(getApplicationContext());
        registerPush();
    }

    /**
     * 注册推送服务
     */
    private void registerPush() {
        pushAgent = PushAgent.getInstance(this);
        pushAgent.register(new IUmengRegisterCallback() {
            @Override
            public void onSuccess(String s) {
                //注册成功返回deviceToken
                pushAgent.setNotificationClickHandler(new UmengNotificationClickHandler() {
                    @Override
                    public void dealWithCustomAction(Context context, UMessage uMessage) {
                        parseAndHandlePushMsg(context, uMessage);
                    }

                });
            }

            @Override
            public void onFailure(String s, String s1) {

            }
        });
        pushAgent.setDebugMode(false);
    }

    /**
     * 解析并处理推送的内容
     *
     * @param context
     * @param msg
     */
    private void parseAndHandlePushMsg(Context context, UMessage msg) {
        try {
            String json = msg.custom;
            if (!StringUtils.isEmpty(json)) {
                Object obj = JsonUtils.parseJsonString(json, PushBean.class);
                if (null != obj) {
                    if (obj instanceof PushBean) {
                        PushBean bean = (PushBean) obj;
                        switch (bean.getObjectCode()) {
                            case "1"://跳转支付详情页面
                                PushBean.CustomObj custom = bean.getObject();
                                Intent intent = new Intent(context, OrderDetailActivity_.class);
                                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                intent.putExtra("orderId", custom.getCode());
                                intent.putExtra("receiveOrSend", custom.getType());
                                context.startActivity(intent);
                                break;
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 弹出系统的Toast
     *
     * @param stringMsg 字符串
     */
    public static void toast(String stringMsg) {
        Message msg = new Message();
        msg.obj = stringMsg;
        mHandler.sendMessage(msg);
    }

    /**
     * 弹出系统的Toast
     *
     * @param stringRes 字符串的ID
     */
    public static void toast(int stringRes) {
        Message msg = new Message();
        msg.obj = mContext.getString(stringRes);
        mHandler.sendMessage(msg);
    }

    // 单例模式中获取唯一的KApplication实例
    public static App getInstance() {
        if (instance == null) {
            instance = new App();
        }
        return instance;
    }

    // 添加Activity到容器中
    public void addActivity(Activity activity) {
        kList.add(activity);
    }

    //移除容器中得Activity
    public void removeActivity(Activity activity) {
        kList.remove(activity);
    }

    // 遍历所有Activity并finish
    public void exit() {
        for (Activity activity : kList) {
            activity.finish();
        }
//        System.exit(0);
    }


}
